<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// 
//  sina_app.php
//  app.6block.com
//  
//  Created by Will_Lee on 2011-06-09.
//  Copyright 2011 lishuzu. All rights reserved.
// 

class Guesswillbecomewhat extends CI_Controller {

	function __construct(){
		parent::__construct();
		//sina微博接入
		$this->load->helper('sinaweibo/sina_weibooauth');
		//加载微博配置
		$this->config->load('app');
		$this->app_config = $this->config->item('app');
		$this->load->library('image_lib');
		$this->appid = 1;
		//关注的人的uid或者昵称
		$this->follow_uid = '2157621611';
		//背景url
		$this->gamebgurl = base_url().'resources/images/app/gwbcw/bg.png';
		$this->gamebgpath = APPPATH.'../resources/images/app/gwbcw/bg.png';
		$this->app_user = $this->session->userdata($this->appid.'_app_user') ? $this->session->userdata($this->appid.'_app_user') : '';
		$this->wbmessage = '哇！原来@'.@$this->app_user['name'].' 下辈子会变成......我正在玩#第六街区# #猜猜你下辈子会变成什么#，地址是： '.$this->app_config[$this->appid]['appurl'];
	}
	function index(){
		$username = trim(addslashes($this->input->post('testname', true)));
		$app_user = $this->app_user;
		$app_keys = $this->session->userdata($this->appid.'_app_keys');
		if ($app_user) {
			$data['user'] = $app_user;
		}
		if ($username) {
			$data['username'] = $username;
		}elseif ($app_user) {
			$data['username'] = $app_user['name'];
		}else{
			$data['username'] = '';
		}
		$data['gamepic'] = $this->_img($username);
		$data['appid'] = $this->appid;
		$data['wbmessage'] = $this->wbmessage;
		$this->load->template('app/app_sina_guesswillbecomewhat_tpl',$data);
	}
	//生成图片
	function _img($username = '')
	{
		if (empty($username)) {
			return $this->gamebgurl;
		}
		$words = array(
			'禽兽','有钱人','比尔盖茨','孙悟空','少妇','上帝','潮人','明星','男的','处儿','美女','美少女战士','葫芦娃','瘦子','大便','狼人',
			'巨人','猫','叮当猫','苍井空','哥斯拉','奥特曼','帅哥','蜘蛛侠','GAY','吸血鬼','超人','混蛋','植物','阿凡达','小怪兽','公主',
			'你的人','蕾丝','凤姐','老人','小萝莉','陈冠希','变形金刚','王子','胖子','好人','傻子','海绵宝宝','女的','贝壳','烈士','套套',
			'领导人','妇科医生','樱桃小丸子','伪娘','古代人','僵尸','球星','神','八戒','钢铁侠','小孩','你妹','外星人','流氓','你的影子','唐僧',
			'动物','死人','加菲猫','怪叔叔','人类','春哥','灰姑娘','自己','透明人','内裤','龙猫','小月月',
			);
		$word_index  = array_rand($words);
		$config['image_library'] = 'gd2';
		$config['source_image'] = $this->gamebgpath;
		$config['new_image'] = APPPATH.'../upload/app/'.$this->appid.'_'.md5($username.$this->appid).'.jpg';
		//图片地址
		$imgurl = base_url().'upload/app/'.$this->appid.'_'.md5($username.$this->appid).'.jpg';
		
		$config['dynamic_output'] = false;
		$config['wm_type'] = 'text';
		$pre_word = "下辈子会变成";
		$back_word = $words[$word_index];
		//词语最长长度
		$lenth_arr = array(strlen($back_word) , strlen($username), strlen($pre_word));
		sort($lenth_arr,SORT_NUMERIC);
		$maxlen = array_pop($lenth_arr);
		$space_len = $maxlen;
		
		$str = str_pad($username, $space_len, " ", STR_PAD_BOTH)."\n".
				str_pad($pre_word, $space_len, " ", STR_PAD_BOTH)."\n".
				str_pad($back_word, $space_len, " ", STR_PAD_BOTH)."\n";
		$config['wm_text'] = $str;
		$config['wm_font_path'] = './system/fonts/jjz.ttf';
		$config['wm_font_size'] = '17';
		$config['wm_font_color'] = 'FF0000';
		//阴影
		$config['wm_shadow_color'] = '000000';
		$config['wm_shadow_distance'] = 1;
		//对齐方式
		$config['wm_vrt_alignment'] = 'top';
		$config['wm_hor_alignment'] = 'left';
		$config['wm_vrt_offset'] = '30';
		$config['wm_hor_offset'] = '15';

		if (file_exists($config['new_image'])) {
			return $imgurl;
		}else{
			$this->image_lib->initialize($config);
			if ( ! $this->image_lib->watermark()){
				echo $this->image_lib->display_errors();
			    return false;
			}			
			return $imgurl;			
		}
		return $imgurl;
	}
	
	function towb($username){
		$username = urldecode(trim($username));
		$app_user = $this->app_user;
		
		$app_keys = $this->session->userdata($this->appid.'_app_keys');
		if (!$username) {
			$username = $app_user['name'];
		}
		$config = $this->app_config[$this->appid];
		$message = $this->input->post('wbmessage') ? $this->input->post('wbmessage') : '';
		$imgurl = base_url().'upload/app/'.$this->appid.'_'.md5($username.$this->appid).'.jpg';
		
		if ($this->input->post('release', true) && $message) {
				$sina_client = new WeiboClient( $config['akey'], $config['skey'], $app_keys['last_key']['oauth_token'] , $app_keys['last_key']['oauth_token_secret']  );
				$result = $sina_client->upload($message , $imgurl );
				if ($this->input->post('fllow')) {
					$sina_client->follow($this->follow_uid);					
				}
				if (!$result['error_code']) {
					showmessage('发送成功', $config['appurl'], 2);
				}else{
					showmessage('发送失败，可能是内容重复或者为空', $config['appurl'], 2);
				}
		}
		if ($app_user) {
			$data['user'] = $app_user;
		}
		$data['username'] = $username;
		$data['gamepic'] = $this->_img($username);
		$data['wbmessage'] = $this->wbmessage;
		$this->load->template('app/app_sina_guesswillbecomewhat_towb_tpl',$data);
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */